package com.qianyu.core.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianyu.core.entity.bean.WalletAccount;
import com.qianyu.core.entity.dto.WalletAccountDto;
import com.qianyu.core.enumerate.ResultEnum;
import com.qianyu.core.exception.CustomerException;
import com.qianyu.core.mapper.WalletAccountMapper;
import com.qianyu.core.service.WalletAccountService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class WalletAccountServiceImpl extends ServiceImpl<WalletAccountMapper, WalletAccount> implements WalletAccountService {
    @Override
    public Boolean saveWalletAccount(WalletAccountDto.WalletAccountSaveDto dto) {
        if (getAccount() != null) {
            throw new CustomerException(ResultEnum.WALLET_ACCOUNT_EXIST);
        }
        WalletAccount walletAccount = BeanUtil.copyProperties(dto, WalletAccount.class);
        return save(walletAccount);
    }

    @Override
    public Boolean editWalletAccount(WalletAccountDto.WalletAccountEditDto dto) {
        return update(Wrappers.<WalletAccount>lambdaUpdate()
                .set(ObjectUtil.isNotEmpty(dto.getWalletAccount()), WalletAccount::getWalletAccount, dto.getWalletAccount())
                .set(ObjectUtil.isNotEmpty(dto.getAccountType()), WalletAccount::getAccountType, dto.getAccountType())
                .eq(WalletAccount::getId, dto.getId()));
    }

    @Override
    public WalletAccount getAccount() {
        return getOne(Wrappers.emptyWrapper());
    }
}
